const Util = require('./util')
const logger = require('./logger')
class Message{

    static msgType = {
        'authorization': 'authorization',
        'join': 'join',
        'notification': 'notification',
        'getRouterCapabilities': 'getRouterCapabilities',
        'createTransport':'createTransport',
        'transport-connect': 'transport-connect',
        'producer': 'producer',
        'removeProducer': 'removeProducer',
        'produceData': 'produceData',
        'connectWebRtcTransport': 'connectWebRtcTransport',
        'newConsumer': 'newConsumer',
        'removeConsumer': 'removeConsumer',
        'consumeData' : 'consumeData',
        'memberDestroy': 'memberDestroy',
        'pauseProducer': 'pauseProducer',
        'pauseConsumer': 'pauseConsumer',
        'resumeProducer': 'resumeProducer',
        'resumeConsumer': 'resumeConsumer',
        'error': 'error',
        'ping': 'ping',
        'pong': 'pong',
        'statistics': 'statistics',
        'memberList': 'memberList',
        'sendMessageToPeerId': 'sendMessageToPeerId',
        'receiveMessage': 'receiveMessage',
        'notified': 'notified'
    }

    static decoder(message) {
        const decoder = {}
        try{
            const data = JSON.parse(message);
            decoder.type = data["type"]
            decoder.data = data["data"]
        }catch (e) {
            logger.info('message', e)
            decoder.type = 'error'
            decoder.data = e.toString()
        }
       // logger.info("decoder", decoder)
        return decoder
    }

    static encoder(type, data){
        return JSON.stringify({
            type,
            data
        })
    }


}
module.exports = Message

